## Load data and libraries 
library(tidyverse)
library(haven) 
library(pollster)
library(modelsummary)
library(estimatr) 
library(ggpubr)
library(here)
rm(list=ls())

here::i_am("dot_code_figs/figure-2.R")

anes <- read_csv("dot_code_figs/ANES files/anes_timeseries_cdf_csv_20220916.csv")

anes2 <- anes %>%
  select(c(weight_full = VCF0009z, #weights for full sample
           #weight_mode = VCF0009x, # FTF weights DROP 2020
           #weight_web = VCF0009y, # web weights DROP 2020
           weight_post_full = VCF9999, # post-election full weights
           year = VCF0004, # year
           FT_dem = VCF0218 , # Dem Party FT
           FT_rep = VCF0224, # Rep Party FT
           pid = VCF0301, # PID
           mode = VCF0017 #mode of survey, only want 0 (FTF) or 4 (web) later
  )) %>%
  subset(pid != 4 & pid > 0)%>% #drop pure independents and NAs
  sapply(as.numeric) %>% data.frame()


A24 <-  read_csv("dot_code_figs/ANES files/anes_timeseries_2024_csv_20250430.csv") #2024 

anes24 <- A24 %>% #2024 ANES
  mutate(year = 2024) %>%     
  select(weight_post_full =  V240107b,   # weights, Post full ANES sample (FTF + panel + web + PAPI)
         weight_full =   V240107a, # 
         #weight_ftf = V240101a, # FTF weights DROP 2020
         #weight_web = V240102a, # web weights DROP 2020
         FT_dem = V241166,
         FT_rep = V241167,
         pid =  V241227x,
         mode = V240002a, #only want 1 (FTF) or 2 (web)
         year) %>%
  subset(pid != 4 & pid > 0)%>% #drop pure independents and NAs
  subset(FT_dem >= 0 & FT_rep >=0)%>%
  sapply(as.numeric) %>% data.frame()

anes3 <- bind_rows(anes2, anes24)

#check
summary(anes3$year) #perf

##########################################################################
#create pid dummy
anes3$pid_dum <- ifelse(anes3$pid < 4, "Democrat", "Republican")

#create inparty and outparty
anes3$inparty <- ifelse(anes3$pid_dum=="Democrat", "Democrat", "Republican")
anes3$outparty <- ifelse(anes3$pid_dum=="Democrat", "Republican", "Democrat")

#link FTs to in and out_party
anes3$inparty_FT <- ifelse(anes3$pid_dum=="Democrat", anes3$FT_dem, anes3$FT_rep)
anes3$outparty_FT <- ifelse(anes3$pid_dum=="Democrat", anes3$FT_rep, anes3$FT_dem)


###caclulate AP as IN_FT - OUT_FT
anes3$AP <- anes3$inparty_FT - anes3$outparty_FT




##### panel a: in/out-party means by year
ignore_mode <- anes3 %>%
  filter((year > 1977) & year != 2002 & !is.na(weight_full)) %>% 
  group_by(year) %>% 
  summarise(avg_same = weighted.mean(inparty_FT, weight_full, na.rm=TRUE),
            sd_same = sd(inparty_FT, na.rm=TRUE),
            se_same = sd_same/sqrt(n()),
            avg_other = weighted.mean(outparty_FT, weight_full,na.rm=TRUE),
            sd_other = sd(outparty_FT, na.rm=TRUE),
            se_other = sd_other/sqrt(n())) 

ft_over_time <- ignore_mode %>% 
  gather(key = label, 
         value = FT_Measure,
         c(avg_same,avg_other)) %>%
  gather(key=label2,
         value=se,
         c(se_same,se_other))

ft_over_time <- ft_over_time %>% 
  mutate(target_mode = case_when(
    label == "avg_same" & label2=="se_same" ~ 1, 
    label == "avg_same" & label2=="se_other" ~ 2, 
    label == "avg_other"& label2=="se_same" ~ 3,
    label == "avg_other" & label2=="se_other"~ 4)) %>%
  subset(target_mode==1 | target_mode==4)

pooled <- ft_over_time %>% 
  ggplot(aes(year,FT_Measure, 
             group = label)) + 
  geom_line(aes(color = as.factor(target_mode),
                lty = as.factor(target_mode))) + 
  geom_errorbar(aes(ymin=FT_Measure-1.96*se, ymax=FT_Measure+1.96*se,
                    group=target_mode, color=as.factor(target_mode)), width=1, linewidth=0.5) +
  geom_point(aes(color = as.factor(target_mode))) +   
  scale_color_manual(name = "",
                     values = c("black","darkgray"), 
                     labels = c("In-Party","Out-Party")) + 
  scale_linetype_manual(name = "",
                        values = c("dotted","solid"), 
                        labels = c("In-Party","Out-Party")) + 
  scale_x_continuous(breaks = c(1980,1988,1996,2004,2012,2020)) + 
  ylim(c(0,100)) + 
  xlab("Year") + 
  ylab("Feeling Thermometer Rating") + 
  labs(subtitle="A"
       #caption="ANES Cumulative Data File (N=40,037)"
  )+
  #ggtitle("Partisan Sentiment Over Time") +  
  theme_bw() + 
  #guides(color=guide_legend(nrow=2,byrow=TRUE))+
  theme(legend.position = "bottom",
        panel.grid = element_blank())  




##panel b: in/out-party means by year and mode
mode <- anes3 %>%
  subset((mode==1 & year==2024) | (mode==2 & year==2024) |
           (mode==0 & year != 2024) | (mode==4 & year !=2024))

mode$modes <- ifelse(mode$mode < 2, "FTF", "Web")

mode <- mode %>%
  filter((year > 1977) & year != 2002 & !is.na(weight_full)) %>% 
  group_by(year, modes) %>% 
  summarise(avg_same = weighted.mean(inparty_FT, weight_full, na.rm=TRUE),
            sd_same = sd(inparty_FT, na.rm=TRUE),
            se_same = sd_same/sqrt(n()),
            avg_other = weighted.mean(outparty_FT, weight_full,na.rm=TRUE),
            sd_other = sd(outparty_FT, na.rm=TRUE),
            se_other = sd_other/sqrt(n())) 

ft_over_time_mode <- mode %>% 
  gather(key = label, 
         value = FT_Measure,
         c(avg_same,avg_other)) %>%
  gather(key=label2,
         value=se,
         c(se_same,se_other))
 

## for plotting, need a code that tells you same vs. other party & mode (ftf vs. internet)
ft_over_time_mode <- ft_over_time_mode %>% 
  mutate(target_mode = case_when(
    modes == "FTF" & label == "avg_same" & label2=="se_same" ~ 1, 
    modes == "FTF" & label == "avg_same" & label2=="se_other" ~ 2, 
    modes == "FTF" & label == "avg_other" & label2=="se_same"~ 3, 
    modes == "FTF" & label == "avg_other" & label2=="se_other" ~ 4, 
    modes == "Web" & label == "avg_same" & label2=="se_same"~ 5, 
    modes == "Web" & label == "avg_same" & label2=="se_other"~ 6, 
    modes == "Web" & label == "avg_other"& label2=="se_same" ~ 7,
    modes == "Web" & label == "avg_other" & label2=="se_other"~ 8)) %>%
  subset(target_mode==1 | target_mode==3 | target_mode==5 | target_mode==8)


pooled_mode <- print(ft_over_time_mode %>% 
        ggplot(aes(year,FT_Measure, 
                   group = target_mode)) + 
        geom_line(aes(color = as.factor(target_mode),
                      lty = as.factor(target_mode))) + 
         geom_point(aes(color = as.factor(target_mode))) +   
          geom_errorbar(aes(ymin=FT_Measure-1.96*se, ymax=FT_Measure+1.96*se,
                            group=target_mode, 
                            color=as.factor(target_mode)), width=1, linewidth=0.5) +
          scale_color_manual(name = "",
                           values = c("black","#767676","#aaaaaa","#dddddd"), 
                           labels = c("In-Party (FTF)","Out-Party (FTF)",
                                      "In-Party (Online)","Out-Party (Online)")) + 
        scale_linetype_manual(name = "",
                              values = c("solid","solid","dashed","dashed"), 
                              labels = c("In-Party (FTF)","Out-Party (FTF)",
                                         "In-Party (Online)","Out-Party (Online)")) + 
        scale_x_continuous(breaks = c(1980,1988,1996,2004,2012,2020)) + 
        ylim(c(0,100)) + 
        xlab("Year") + 
        ylab("Feeling Thermometer Rating") + 
        labs(subtitle="A"
             #caption="ANES Cumulative Data File (N=40,037)"
        )+
        #ggtitle("Partisan Sentiment Over Time") +  
        theme_bw() + 
        guides(color=guide_legend(nrow=2,byrow=TRUE))+
        theme(#plot.title = element_text(hjust=0.5),
          legend.position = "bottom",
          panel.grid = element_blank())) 

##panel c: AP by mode (in - out)
ap <- anes3 %>%
  subset((mode==1 & year==2024) | (mode==2 & year==2024) |
           (mode==0 & year != 2024) | (mode==4 & year !=2024))

ap$modes <- ifelse(ap$mode < 2, "FTF", "Web")

ap_count <- ap %>%
  filter((year > 1977) & year != 2002 & !is.na(weight_full)) %>% 
  group_by(year, modes) %>%
  summarise(count=n())

ap <- ap %>%
  filter((year > 1977) & year != 2002 & !is.na(weight_full)) %>% 
  group_by(year, modes) %>% 
  summarise(av_ap = weighted.mean(AP, weight_full, na.rm=TRUE),
            sd_ap = sd(AP, na.rm=TRUE),
            se_ap = sd_ap/sqrt(n()))


p2 <- print(ap %>%  
              select(c(year,modes,av_ap, se_ap)) %>% 
              ggplot(aes(year,av_ap, 
                         group = modes)) + 
              geom_line(aes(color = as.factor(modes),
                            lty = as.factor(modes))) + 
              geom_point(aes(color = as.factor(modes))) +   
              scale_color_manual(name = "",
                                 values = c("#474747","grey"), 
                                 labels = c("FTF","Online")) + 
              scale_linetype_manual(name = "",
                                    values = c("solid","dashed"), 
                                    labels = c("FTF","Online")) + 
              geom_errorbar(aes(ymin=av_ap-se_ap*1.96, ymax=av_ap+se_ap*1.96,
                                group=as.factor(modes), color=as.factor(modes)),
                            width=1, linewidth=0.5)+
              scale_x_continuous(breaks = c(1980,1988,1996,2004,2012,2020)) + 
              ylim(c(0,100)) + 
              xlab("Year") + 
              ylab("") + 
              labs(subtitle="B",
                   caption="ANES Cumulative Data File (N=40,037)"
              )+
              #ggtitle("Partisan Sentiment Over Time") +  
              theme_bw() + 
              #guides(color=guide_legend(nrow=2,byrow=TRUE))+
              theme(#plot.title = element_text(hjust=0.5),
                legend.position = "bottom",
                panel.grid = element_blank()))  

png(file = "dot_code_figs/figs/figure-2.png",
    height = 6,
    width = 11,
    units = "in",       # Use inches to match pdf dimensions
    res = 300)          # Set resolution (DPI) for high-quality output

print(ggarrange(#pooled,
                pooled_mode, 
                p2,
                ncol = 2, nrow = 1))  

dev.off()
